from django.core.cache import caches
from django.core.paginator import Paginator, Page
from django.http import HttpResponse
from django.shortcuts import render
from .models import Book
from django.views.decorators.cache import cache_page

# Create your views here.

def books(request):
    bs = Book.objects.all().order_by('id')
    paginater = Paginator(bs, 10)
    page_num = request.GET.get("page", 1)
    page = paginater.get_page(page_num)
    return render(request, "main/books.html", context={"page": page})


def middleware(request):
    print(request.user)
    print("视图函数")
    return HttpResponse("恭喜你，访问成功")


@cache_page(timeout=60)
def cache(request):
    """
    加入页面内容并不会随时更新，没用必要每次都去执行一些利复杂逻辑
    """
    # cache = caches['default']
    # print(cache.get("qiku"),"-------")
    # cache.set("qiku", "hehe", 600)
    # print(cache.get("qiku"), "+++++")

    # redis = caches['default']
    # print(redis.get("qiku"), "-------")
    # redis.set("qiku", "hehe", 600)
    # print(redis.get("qiku"), "+++++")


    first_book = Book.objects.first()
    print(f"查询处理数据复杂逻辑{first_book.title}")
    return HttpResponse("缓存使用")